package java8_23.market;

/**
 * @author oldliu
 * @since 1.0
 */
//超市类
public class SuperMarket {
    String name = "家乐福";
    String tele = "66666666";
    Vip[] huiyuan = new Vip[100];

    //新会员加入
    public boolean add(Vip v) {
        for (int i = 0; i < huiyuan.length; i++) {
            if (huiyuan[i] == null) {
                huiyuan[i] = v;
                return true;
            }
        }
        return false;
    }

    public boolean add(String cardNo, String name, String phone, int jifen) {
        for (int i = 0; i < huiyuan.length; i++) {
            if (huiyuan[i] == null) {
                //先创建对象，
                //对象赋值，
                //对象加入到超市中，放入这个数组
                Vip v = new Vip();
                v.name = name;
                v.cardNo = cardNo;
                v.telephone = phone;
                v.integal = jifen;

                huiyuan[i] = v;
                return true;
            }
        }
        return false;
    }

    public boolean inc(String cardNo, int fen) {
        for (int i = 0; i < huiyuan.length; i++) {
            if (huiyuan[i] == null) {
                return false;
            }
            if (cardNo.equals(huiyuan[i].cardNo)) {
                huiyuan[i].integal += fen;
                return true;//
            }
        }
        return false;
    }

    public String exchange(String cardno, int type) {
        for (int i = 0; i < huiyuan.length; i++) {
            if (huiyuan[i] == null)
                return null;

            if (huiyuan[i].cardNo.equals(cardno)) {
                switch (type) {
                    case 1:
                        if (huiyuan[i].integal >= 100) {
                            huiyuan[i].integal -= 100;
                            return "兑换了餐巾纸";
                        }
                        return null;
                    case 2:
                        if (huiyuan[i].integal >= 400) {
                            huiyuan[i].integal -= 400;
                            return "兑换了剃须刀一个";
                        }
                        return null;
                    case 3:
                        if (huiyuan[i].integal >= 1000) {
                            huiyuan[i].integal -= 1000;
                            return "兑换了炒锅一个";
                        }
                        return null;
                    default:
                        System.out.println("没有此类型的商品");
                        return null;
                }
            }
        }
        return null;
    }
    //积分最高的会员
    public Vip findMax(){
        if(huiyuan[0]==null)
            return null;

        int max=huiyuan[0].integal;
        int index=0;
        for(int i=0;i<huiyuan.length;i++){
            if(huiyuan[i]!=null &&huiyuan[i].integal>max){
                max=huiyuan[i].integal;
                index=i;
            }
        }
        return huiyuan[index];
    }

    public void showInfo() {
        System.out.println("本超市名叫：" + name + "，电话：" + tele);
        System.out.println("会员卡如下：");
        for (int i = 0; i < huiyuan.length; i++) {
            if (huiyuan[i] == null)
                return;
            huiyuan[i].show();//不需要else,土
        }
    }
}
